<?php

/**
 * Description of ResourceController
 *
 * @author gun
 */
require_once 'BaseController.php';
require_once 'ResourceParameterController.php';
class Admin_ResourceController extends Admin_BaseController {    
    
    public function init() {
        parent::init();        
        
    }
    
    public function indexAction()
    {
        $request = $this->getRequest();                

        if ($this->httpRequest()->isXmlHttpRequest()) {
            $q = $request->getParam('q');
            $page = $request->getParam('page');
            
            $where = array('name LIKE ?'=>"%$q%");
            $stat = $this->_helper->ListPage('Admin_Model_ResourceService', $page, $where);            
            
            $options = array(
                'limit'=>
                    array(
                        $stat['perpage'] => $stat['offset'])
            );
            
            $service = AppRegistry::loadService('Admin_Model_ResourceService');
            $data = $service->getAll($where, $options);
            
            if ($data) {
                $this->_helper->json(array(
                    'data' => $data,
                    'pview'=> $stat['pview'],
                    'offset'=> $stat['offset'],
                    'fieldname' => 'category'
                    ));
                
            } else {
                $this->_helper->json(array('error'=>'No Data Found.'));
            }
        }
    }
    
    public function saveAction() {
        $id = $this->getParam('id');
        $name = $this->getParam('name');
        $category = $this->getParam('category');
        $desc = $this->getParam('desc');
        $resource_url = $this->getParam('resource_url');
        $example = $this->getParam('example');
        $apiversion = $this->getParam('api_version');
        $httpmethod = $this->getParam('http_method');
        $auth = $this->getParam('auth');
        $response = $this->getParam('response_fmt');
        $metaflag = $this->getParam('metaflag');
        $tags = $this->getParam('tags');
        
        $data = array(
            'resID'=>$id,
            'name'=>$name,
            'category'=>$category,
            'description'=>$desc,
            'resource_url'=>$resource_url,
            'example'=>$example,
            'tags'=>$tags
        );
        
        if ("true" === $metaflag)
            $meta = array(
                'resID' => $id,
                'api_version' => $apiversion,
                'http_method' => $httpmethod,
                'family' => $category,
                'auth' => $auth,
                'response_fmt' => $response
            );
        
        foreach($this->baseUserLogin($id) as $key => $value) $data[ $key ] = $value;
        
        try {
            $service = AppRegistry::loadService('Admin_Model_ResourceService');            
            $saveResource = (!isset($id) ? $service->doSave($data) : $service->doUpdate($data, $id));   
            
            if ("true" === $metaflag) {
                $check = $service->checkMeta(array('resID = ?' => $id));
                $saveMeta = (('0' == $check) ? $service->saveMeta($meta) : $service->updateMeta($meta, $id));
            }
            $this->_helper->json(array('status'=>(($saveResource || ($saveResource && isset($saveMeta) && $saveMeta)) 
                    ? 'Save Success.' : 'Nothing changed.')));
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getMessage()));            
        }        
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function editAction() {
        $id = $this->getParam('id');
        $service = AppRegistry::loadService('Admin_Model_ResourceService');
        $data = $service->getRow($id);

        $catService = AppRegistry::loadService('Admin_Model_CategoryService');
        $category = $catService->getAllCategory();

        $this->view->category = $this->_helper->CatList($category, $data['category']);
        $this->view->data = $data;        
    }
    
    public function addAction() {
        $service = AppRegistry::loadService('Admin_Model_CategoryService');
        $category = $service->getAllCategory();
        
        $this->view->category = $this->_helper->CatList($category);
    }

    public function deleteAction() {
        try {
            $id = $this->getParam('id');
            $service = AppRegistry::loadService('Admin_Model_ResourceService');
        
            $delete = $service->doDelete($id);
            $this->_helper->json(array('status'=>($delete ? 'Delete success.' : 'Nothing deleted.')));
            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getTraceAsString()));
        }
    }        
    
}
